package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.RoomBean;
import com.project.bean.RoomSetBean;
import com.project.mapper.IRoomMapper;
import com.project.mapper.IRoomSetMapper;
import com.project.service.IRoomSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoomSetServiceImpl implements IRoomSetService {
    @Autowired
    private IRoomMapper roomMapper;
    @Autowired
    private IRoomSetMapper roomSetMapper;
    @Override
    public void add(RoomSetBean roomSetObj) {
        roomSetMapper.insert(roomSetObj);
        RoomBean room = roomMapper.selectById(roomSetObj.getRoomId());
        room.setStatus("设施损坏");
        roomMapper.updateById(room);
    }

    @Override
    public List<RoomSetBean> findByRoom(int roomId) {
        QueryWrapper<RoomSetBean> qw = new QueryWrapper<>();
        qw.eq("fk_roomId",roomId);
        return roomSetMapper.selectList(qw);
    }

    @Override
    public void pressRoomSet(int roomId, int roomSetId) {
        //得到设施ID对应设施对象
        RoomSetBean setBean = roomSetMapper.selectById(roomSetId);
        setBean.setIsSolve("已解决");
        roomSetMapper.updateById(setBean);

        QueryWrapper<RoomSetBean> qw = new QueryWrapper<>();
        qw.eq("s_isSolve","未解决");
        qw.eq("fk_roomId",roomId);

        Integer num = roomSetMapper.selectCount(qw);
        if(num == 0){
            RoomBean room = roomMapper.selectById(roomId);
            room.setStatus("正常");
            roomMapper.updateById(room);
        }
    }
}
